package com.china08.yxyapi.api.home.activity.res;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.lecture.YxyHuodongResResultTea;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.repository.db.lectureresult.YxyHuodongResResultTeaRepository;

/**
 * 哈尔滨市按学校统计
 *
 */
@CrossOrigin
@RestController
public class ResResultTeaController extends YxyApiBaseController {

    @Autowired
    private YxyHuodongResResultTeaRepository teaRepository;

    @RequestMapping(value = "/api_yw/home/activity/res/result/tea", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<YxyHuodongResResultTea> process(HttpServletRequest request, //
            HttpServletResponse response, //
            @RequestBody(required = false) ReqModel reqModel) {
        Page<YxyHuodongResResultTea> teaPage = null;
        String desc = "DESC";
        String paixu = "resCount";
        String areaIdDistrict = "610203";
        if (StringUtils.isNotBlank(reqModel.desc)) {
            desc = reqModel.desc.startsWith("asc") ? "ASC" : "DESC";
        }
        if (StringUtils.isNotBlank(reqModel.paixu)) {
            paixu = reqModel.paixu;
        }
        if (StringUtils.isNotBlank(reqModel.areaIdDistrict)) {
            areaIdDistrict = reqModel.areaIdDistrict;
        }
        Pageable pageable = reqModel.getPageable(Direction.valueOf(desc), paixu);
        if (StringUtils.isNotBlank(reqModel.teaName)) {
            teaPage = teaRepository.findByAreaIdDistrictAndTeaNameContaining(areaIdDistrict, reqModel.teaName,
                    pageable);
        } else {
            teaPage = teaRepository.findByAreaIdDistrict(areaIdDistrict, pageable);
        }
        return teaPage;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String teaName;
        public String paixu;
        public String desc;
        public String areaIdDistrict;
    }
}
